package me.zhengjie.modules.company.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.base.ApiResult;
import me.zhengjie.modules.banner.domain.Banner;
import me.zhengjie.modules.company.domain.Company;
import me.zhengjie.modules.company.service.CompanyService;
import me.zhengjie.modules.company.service.dto.CompanyDto;
import me.zhengjie.modules.company.service.dto.CompanyQueryParam;
import me.zhengjie.modules.dcompany.domain.DependentCompany;
import me.zhengjie.modules.dcompany.service.DependentCompanyService;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
* @author TianI
* @date 2022-01-21
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "CompanyController管理")
@RequestMapping("/api/company")
public class CompanyController {

    private final CompanyService companyService;

    private final DependentCompanyService dCompanyService;
    @GetMapping("/getDcompanyById/{companyId}")
    public ApiResult getDcompanyById (@PathVariable("companyId") String companyId){
        List<DependentCompany> dependentCompany = dCompanyService.getDcompanyById(companyId);
        return ApiResult.ok(dependentCompany);
    }

    @GetMapping("getCompanyInfo/{companyId}")
    public ApiResult getCompanyInfo (@PathVariable("companyId") Long companyId){
        Company byId = companyService.getById(companyId);
        return ApiResult.ok(byId);
    }
    @GetMapping
    @Log("查询CompanyController")
    @ApiOperation("查询CompanyController")
    @PreAuthorize("@el.check('company:list')")
    public ResponseEntity query(CompanyQueryParam query, Pageable pageable){
        return new ResponseEntity<>(companyService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增CompanyController")
    @ApiOperation("新增CompanyController")
    @PreAuthorize("@el.check('company:add')")
    public ResponseEntity create(@Validated @RequestBody CompanyDto resources){
        return new ResponseEntity<>(companyService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改CompanyController")
    @ApiOperation("修改CompanyController")
    @PreAuthorize("@el.check('company:edit')")
    public ResponseEntity update(@Validated @RequestBody CompanyDto resources){
        companyService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除CompanyController")
    @ApiOperation("删除CompanyController")
    @PreAuthorize("@el.check('company:del')")
    public ResponseEntity delete(@RequestBody Set<Long> ids) {
        companyService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

//    @PostMapping("/uploadBanner")
//    public ResponseEntity<Object> uploadBanner(@RequestParam("file") MultipartFile file) {
//        Company company = companyService.createBanner(file);
//        return new ResponseEntity<>(company.getCompanyBannerUrl(),HttpStatus.CREATED);
//    }
    @PostMapping("/uploadImg")
    public ResponseEntity<Object> importProcessData(@RequestParam("file") MultipartFile file) {
        Company company = companyService.createImg(file);
        return new ResponseEntity<>(company.getCompanyBrandLogoUrl(),HttpStatus.CREATED);
    }
    @PostMapping("/uploadVideo")
    public ResponseEntity<Object> uploadVideo(@RequestParam("file") MultipartFile file) {
        Company company = companyService.createVideo(file);
        return new ResponseEntity<>(company.getCompanyVideoUrl(),HttpStatus.CREATED);
    }


    @Log("导出CompanyController")
    @ApiOperation("导出CompanyController")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('company:list')")
    public void download(HttpServletResponse response, CompanyQueryParam query) throws IOException {
        companyService.download(companyService.queryAll(query), response);
    }

}
